<div class="fsh-rightPanel">
    <input type="hidden" id="courseId" value="<%=course_id%>">
    <label> <input type="password" style="display: none"></label>
    <div class="layui-anim layui-anim-upbit">
        <div class="layui-form-item wx-search" id="list_form">
            <div class="layui-inline">
                <input type="text" name="nickname" placeholder="请输入学校名称" autocomplete="off" class="layui-input">
            </div>
            <button class="layui-btn btn-primary" id="search">搜索</button>
        </div>
        <table id="schoolList" class="layui-hide" lay-filter="schoolList"></table>
    </div>
</div>
<script type="text/html" id="toolBar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add_school">+添加学校</button>
    </div>
</script>
<script>
    var courseId = $('#courseId').val();
    layui.use(['layer', 'table'], function () {
        var layer = layui.layer;
        var table = layui.table;
        var tableObj2 = table.render({
            elem: '#schoolList',
            url: context + '/api/schoolCourse/getCourse?course_id=' + courseId,
            //开启头部工具栏，并为其绑定左侧模板，
            toolbar: '#toolBar',
            defaultToolbar: [],
            request: {
                limitName: 'pageSize' //每页数据量的参数名，默认：limit
            },
            align: 'center'
            , cols: [[ //表头
                {type:'numbers'},
                {field: 'school_name', title: '学校名称', align: 'center'},
                {
                    field: 'lock', title: '操作', width: 240, align: 'center', unresize: true,
                    templet: function (d) {
                        return '<a href="javascript:;" class="font-primary" lay-event="del">删除</a>';
                    }
                }
            ]]
            , page: true

        });

        // 检索
        $("#search").click(function () {
            reloadTable(tableObj2);
        });

        //头工具栏事件
        table.on('toolbar(schoolList)', function (obj) {
            var layEvent = obj.event; //获得 lay-event 对应的值
            // 添加
            if (layEvent === 'add_school') {
                showBigDialog({
                    title: '添加学校'
                    , template: 'school'
                    , yes: function (index, layero) {
                        var checkData = table.checkStatus('schoolTable').data;
                        layer.close(index);
                        var data = {};
                        for (var i = 0; i < checkData.length; i++) {
                            data.school_id = checkData[i].id;
                            data.course_id = courseId;
                            $.ajax({
                                url: context + '/api/schoolCourse/insert',
                                type: 'POST',
                                async: false,
                                dataType: 'json',
                                contentType: "application/json;charset-UTF-8",
                                data: JSON.stringify(data),
                                success: function (result) {
                                    if (result.success) {
                                        layer.msg("添加成功");
                                        reloadTable(tableObj2);
                                    } else {
                                        layer.msg(result.msg);
                                    }
                                },
                                error: function () {
                                    layer.msg("添加异常");
                                }
                            })
                        }
                    }

                })
            }
        });
        //表格内部操作按钮监听
        table.on('tool(schoolList)', function (obj) { //注：tool是工具条事件名，mainList是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值
            // 删除
            if (layEvent === 'del') {
                layer.confirm('确认移除此学校？', {
                    icon: 7,
                    title: "提示",
                    btn: ['确认', '取消'] //按钮
                }, function () {
                    $.ajax({
                        url: context + '/api/schoolCourse/'+data.id,
                        type: 'DELETE',
                        success: function (result) {
                            if (result.success) {
                                layer.msg("移除成功");
                                reloadTable(tableObj2);
                            } else {
                                layer.msg(result.msg);
                            }
                        },
                        error: function () {
                            layer.msg("移除异常");
                        }
                    })
                }, function () {
                    layer.closeAll();
                });
            }
        });

    });
</script>
